package com.csdj.t309.common;

import com.csdj.t309.entity.TSysUser;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TSysUserConvertor implements Converter<String, TSysUser> {
    @Override
    public TSysUser convert(String s) {
        TSysUser user = new TSysUser();
        Class<? extends TSysUser> aClass = user.getClass();
        String[] splits = s.split(",");
        for(String split: splits){
            String[] kvs = split.split(":");
            try {
                Method method = aClass.getDeclaredMethod("set" + StringUtils.capitalize(kvs[0]),String.class);
                method.invoke(user,kvs[1]);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return user;
    }

    public static void main(String[] args) {
        String str = "account:cloud,realName:李四,password:12345654678";
        TSysUserConvertor convertor = new TSysUserConvertor();
        TSysUser convert = convertor.convert(str);
        System.out.println(convert.toString());
    }
}
